package com.his.struts.dao.friendlink;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.his.struts.dao.foods.FoodsImp;
import com.his.struts.form.FriendlinkForm;
import com.his.struts.util.DbUtil;

/**
 * 友情鏈接
 * 
 * @author liumiaoyue
 * 
 */
public class FriendLinkImp implements FriendLinkDao {
	Logger log = Logger.getLogger(FriendLinkImp.class);
	DbUtil db = new DbUtil();

	/**
	 * 得到所有鏈接
	 * 
	 * @return List<FriendlinkForm> 友情鏈接集合
	 */
	public List<FriendlinkForm> getLinkBean(ResultSet rs) {
		List<FriendlinkForm> list = new ArrayList<FriendlinkForm>();
		try {
			while (rs.next()) {
				FriendlinkForm form = new FriendlinkForm();
				int fid = rs.getInt("fid");
				String rName = rs.getString("relateName");
				String rAddress = rs.getString("relateAddress");
				String cName = rs.getString("recommendName");
				String cAddress = rs.getString("recommendAddress");
				form.setFid(fid);
				form.setRelateName(rName);
				form.setRelateAddress(rAddress);
				form.setRecommendName(cName);
				form.setRecommendAddress(cAddress);
				list.add(form);
			}
		} catch (SQLException e) {
			log.error(e.getMessage());
		} finally {
			db.closeAll();
		}
		return list;
	}
	
	/**
	 * 得到所有鏈接
	 * 
	 * @return List<FriendlinkForm> 友情鏈接集合
	 */
	public List<FriendlinkForm> getAllLink() {
		List<FriendlinkForm> list = new ArrayList<FriendlinkForm>();
		String sql = "select * from friendlink";
		ResultSet rs = db.executeQuery(sql, null);
		try {
			while (rs.next()) {
				FriendlinkForm form = new FriendlinkForm();
				int fid = rs.getInt("fid");
				String rName = rs.getString("relateName");
				String rAddress = rs.getString("relateAddress");
				String cName = rs.getString("recommendName");
				String cAddress = rs.getString("recommendAddress");
				form.setFid(fid);
				form.setRelateName(rName);
				form.setRelateAddress(rAddress);
				form.setRecommendName(cName);
				form.setRecommendAddress(cAddress);
				list.add(form);
			}
		} catch (SQLException e) {
			log.error(e.getMessage());
		} finally {
			db.closeAll();
		}
		return list;
	}

	/**
	 * 刪除鏈接
	 * 
	 * @param fid
	 *            鏈接id
	 * @return boolean
	 */
	public boolean delLink(int fid) {
		boolean ok = false;
		ArrayList list = new ArrayList();
		String sql = "delete from friendlink where fid=?";
		list.add(fid);
		int i = db.executeUpdate(sql, list);
		if (i != 0) {
			ok = true;
		}
		db.closeAll();
		return ok;
	}

	/**
	 * 添加鏈接
	 * 
	 * @param form
	 *            友情鏈接bean
	 * @return boolean
	 */
	public boolean addLink(FriendlinkForm form) {
		boolean ok = false;
		ArrayList list = new ArrayList();
		String sql = "insert into friendlink(relateName,relateAddress,recommendName,recommendAddress) values(?,?,?,?)";
		list.add(form.getRelateName());
		list.add(form.getRelateAddress());
		list.add(form.getRecommendName());
		list.add(form.getRecommendAddress());
		int i = db.executeUpdate(sql, list);
		if (i != 0) {
			ok = true;
		}
		db.closeAll();
		return ok;
	}

	/**
	 * 更新鏈接
	 * 
	 * @param form友情鏈接bean
	 * @return boolean
	 */
	public boolean updateLink(FriendlinkForm form) {
		boolean ok = false;
		ArrayList list = new ArrayList();
		String sql = "update friendlink set relateName=?,relateAddress=?,recommendName=?,recommendAddress=? where fid=?";
		list.add(form.getRelateName());
		list.add(form.getRelateAddress());
		list.add(form.getRecommendName());
		list.add(form.getRecommendAddress());
		list.add(form.getFid());
		int i = db.executeUpdate(sql, list);
		if (i != 0) {
			ok = true;
		}
		db.closeAll();
		return ok;
	}

}
